package com.rivercloud.dao.Impl;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.rivercloud.dao.ProjectDao;
import com.rivercloud.entity.Project;
import com.rivercloud.entity.User;

public class ProjectDaoImpl implements ProjectDao{

	 static SqlSessionFactory sqlSessionFactory = null;
	 static{
		 SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder();
		 InputStream inputStream =null;
		 
		 try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (Exception e) {
			e.printStackTrace();
		} 
		sqlSessionFactory = sessionFactoryBuilder.build(inputStream);
	 }
	
	@Override
	public void addProject(Project project) {
		SqlSession session = sqlSessionFactory.openSession();
		session.insert("addProject",  project);
		session.commit();
		session.close();
		
	}

	@Override
	public int findIdByName(String projectName) {
		SqlSession session = sqlSessionFactory.openSession();
		Project project = session.selectOne("findProjectByName",projectName);
		session.close();
		return project.getId();
	}
	
	@Override
	public Project findProjectById(int projectId) {
		SqlSession session = sqlSessionFactory.openSession();
		Project project = session.selectOne("findProjectById",projectId);
		session.close();
		return project;
	}

	@Override
	public Project checkProjectName(String projectName) {
		SqlSession session = sqlSessionFactory.openSession();
		Project project = session.selectOne("findProjectByName",projectName);
		session.close();
		return project;
	}

	@Override
	public List<Project> findProjects() {
		SqlSession session = sqlSessionFactory.openSession();
		List<Project> list = session.selectList("findProjects");
		session.close();
		return list;
	}

	@Override
	public void deleteProject(Integer projectId) {
		SqlSession session = sqlSessionFactory.openSession();
		session.delete("deleteProject", projectId);
		
		session.commit();
		session.close();
		
	}

	@Override
	public void updateMemberNum(Integer projectId,Integer member) {
		SqlSession session = sqlSessionFactory.openSession();
		
		Project project = new Project();
		project.setId(projectId);
		project.setMemberNum(member);
		session.update("updateMemberNum",project);
		
		session.commit();
		session.close();
	}

	@Override
	public List<Project> findFinishProjects() {
		SqlSession session = sqlSessionFactory.openSession();
		List<Project> list = session.selectList("findFinishProjects");
		session.close();
		return list;
	}

	@Override
	public List<Project> findUnFinishProjects() {
		SqlSession session = sqlSessionFactory.openSession();
		List<Project> list = session.selectList("findUnFinishProjects");
		session.close();
		return list;
	}

	@Override
	public List<Project> findProjectByIds(List<Integer> list1) {
		SqlSession session = sqlSessionFactory.openSession();
		List<Project> projects = session.selectList("findProjectByIds",list1);
		session.close();
		return projects;
	}

	@Override
	public void addProjectDesc(Integer projectId, String projectDesc) {
		SqlSession session = sqlSessionFactory.openSession();
		
		Project project = new Project();
		project.setId(projectId);
		project.setProjectDesc(projectDesc);
		session.update("addProjectDesc",project);
		
		session.commit();
		session.close();
		
	}

}
